package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.insurance;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.AcceptRequestDataModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.CustInfoModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;

import java.util.Date;
import java.util.List;

public class Net010InsuranceStrategy implements InsuranceStrategy {
    @Override
    public InsuredModel checkInsuredRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository, ProductModel productModel, ProductConfig productConfig) {
        //客户信息
        CustInfoModel custInfoModel = acceptRequestDataModel.getRequest().getCustInfo();
        //获取被保人
        List<InsurantModel> insuredModelList = custInfoModel.getInsurants().getInsurant();
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(productModel.getProdInsSeq(), insuredModelList);
        //获取被保人年龄
        Date validate = acceptRequestDataModel.getRequest().getPolicyInfo().getCvalidate();
        Date birthday = insurantModel.getBirthday();
        int years = DateUtil.getYears(birthday,validate);
        //年龄小于18岁，投保人为父母
        if (years < UnderWriteConstant.AGE_EIGHTEEN_YEAR) {
            if (!(AcceptConstant.PARENTAL_RELATIONSHIP.contains(insurantModel.getRelToApp()))) {
                return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                        String.format(UnderWriteConstant.UNDER_WRITE_NUGG0041_MESSAGE));
            }
        }
        //年龄大于等于18岁，投保人为被投保人的配偶、父母、子女
        else if (years >= UnderWriteConstant.AGE_EIGHTEEN_YEAR) {
            if (!(productConfig.getRelationList().contains(insurantModel.getRelToApp()))) {
                return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                        String.format(UnderWriteConstant.UNDER_WRITE_NUGG0007_MESSAGE));
            }
        }
        //职业类别必须为1-6
        if (!(AcceptConstant.OCCUPATION_ONE_TO_SIX.contains(insurantModel.getOccupationType()))) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_NPGG0028_MESSAGE, insurantModel.getOccupationType()));
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE,
                String.format(AcceptConstant.RESULT_SUCCESS_MESSAGE));
    }
}
